﻿using LdtDataEngin.ModuleDetail.BaseModule.Optimization;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using System.Collections.Generic;
using System.Linq;

namespace LdtDataEngin.ModuleDetail.OptimizationModule
{
    /// <summary>
    /// 数据优选操作
    /// </summary>
    public class OptimizationData: OptimizationBase
    {
        public int IsStage = 0;
        /// <summary>
        /// 数据优选操作
        /// </summary>
        /// <param name="dataModelAsis">分析完毕数据</param>
        /// <param name="dataModelOP">上次优选结果数据</param>
        /// <param name="xmllableConfig">优选配置</param>
        public void OptimizationDataRun(DataModel dataModelAsis, DataModel dataModelOP, Xmllable xmllableConfig, OptimizeConfig optimizeCon, List<DataModel> dataModelSingOpList, List<DataModel> dataModelAsisList)
        {
            if (dataModelAsis.sourceData.sourceList!=null  &&  dataModelAsis.sourceData.sourceList.Count>0 && dataModelAsis.sourceData.sourceList.FirstOrDefault(a=> a.IsCheck=="1")==null)
            {
                dataModelAsis.sourceData.sourceList[0].IsCheck = "1";
                if (dataModelAsis.AttributesList!=null  && dataModelAsis.AttributesList.Count>0)
                {
                    foreach (var attli in dataModelAsis.AttributesList)
                    {
                        if (attli.sourceList!=null && attli.sourceList.Count>0 && attli.sourceList.FirstOrDefault(a=> a.IsCheck=="1")==null)
                        {
                            attli.sourceList[0].IsCheck = "1";
                        }
                    }
                }
            }

            if (dataModelOP == null || dataModelOP.ID==null)
            {
                string xmlpath = "";
                List<string> listvalue = xmllableConfig.Xmlpath.Split('/').ToList();
                listvalue.RemoveAt(xmllableConfig.Xmlpath.Split('/').Length - 1);
                xmlpath = string.Join("/", listvalue);
                string parentid = "";
                if (!string.IsNullOrWhiteSpace(dataModelAsis.ParentId))
                {
                    parentid = dataModelSingOpList.FirstOrDefault(a => a.XmlPath == xmlpath).ID;
                }
                List<DataModel> dataModelAsList = new List<DataModel>();
                dataModelAsList = SubordinateDataModelList(dataModelAsisList, dataModelAsis);
                dataModelAsList.FirstOrDefault(a => a.XmlPath == dataModelAsis.XmlPath).ParentId = parentid;
                dataModelSingOpList.AddRange(dataModelAsList);
            }
            else
            {
                string soursnameop, soursnameasis;
                if (!string.IsNullOrEmpty(DataSourceResult) && !string.IsNullOrEmpty(DataSourceParent))
                {
                    soursnameop = DataSourceResult;
                    soursnameasis = DataSourceParent;
                }
                else
                {
                    if (dataModelOP.sourceData?.sourceList.FirstOrDefault(a => a.IsCheck == "1") == null && dataModelOP.sourceData?.sourceList.Count > 0)
                    {
                        dataModelOP.sourceData.sourceList[0].IsCheck = "1";
                    }
                    soursnameop = dataModelOP.sourceData?.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode;
                    //问题逻辑@@## 行政处罚
                    if (dataModelAsis.sourceData?.sourceList.FirstOrDefault(a => a.IsCheck == "1") == null && dataModelAsis.sourceData?.sourceList.Count > 0)
                    {
                        dataModelAsis.sourceData.sourceList[0].IsCheck = "1";
                    }
                    soursnameasis = dataModelAsis.sourceData?.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode;
                   
                   
                }
                string soursnameopid = optimizeCon.Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code.ToLower() == soursnameop?.ToLower())?.id.ToString();
                string soursnameasisid = optimizeCon.Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code.ToLower() == soursnameasis?.ToLower())?.id.ToString();
                int opsum = ListByCount(xmllableConfig.Priority.Split('>').ToList(), soursnameopid);
                int assum = ListByCount(xmllableConfig.Priority.Split('>').ToList(), soursnameasisid);

                if (xmllableConfig.Type == "a")
                {
                    SginOptimizationData sginOptimizationData = new SginOptimizationData(dataContext) { IsStage=IsStage};
                    sginOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);

                }
                else if (xmllableConfig.Type == "b")
                {
                    SginOptimizationData sginOptimizationData = new SginOptimizationData(dataContext){ IsStage=IsStage};
                    sginOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
                }
                else if (xmllableConfig.Type == "c")
                {
                    SpecifiedDataOptimizationData specifiedDataOptimizationData = new SpecifiedDataOptimizationData(dataContext) { IsStage = IsStage }; ;
                    specifiedDataOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
                }
                else if (xmllableConfig.Type == "d")
                {
                    QuantityOptimizationData quantityOptimizationData = new QuantityOptimizationData(dataContext) { IsStage = IsStage }; ;
                    quantityOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
                }
                else if (xmllableConfig.Type == "e")
                {
                    SginOptimizationData sginOptimizationData = new SginOptimizationData(dataContext) { IsStage = IsStage }; ;
                    sginOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
                }
                else if (xmllableConfig.Type == "f")
                {
                    SingleDataSourceOptimizationData singleDataSourceOptimizationData = new SingleDataSourceOptimizationData(dataContext) { IsStage = IsStage }; ;
                    singleDataSourceOptimizationData.Optimization(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
                }

            }
        }
    }
}
